<?php
/**
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 */

require('includes/application_top.php');

if(isset($_POST['reinstall'])){

  $sql = "DELETE FROM ".TABLE_MODULES_SCHEDULED." WHERE module='".$_POST['module']."' AND version='".$_POST['version']."'";

  $db->Execute($sql);

}

if(isset($_POST['module'])){

  // status "S" for scheduled

  $sql = "INSERT INTO ".TABLE_MODULES_SCHEDULED." (module, version, status) VALUES ('".$_POST['module']."', '".$_POST['version']."', 'S')";

  $db->Execute($sql);

  $sql = "UPDATE `".TABLE_CONFIGURATION."` SET configuration_value='".(time()-25*60*60)."' WHERE configuration_key='MODULE_AUTO_UPDATE_TIME'";

  $db->Execute($sql);

  header('Location: '.zen_href_link(FILENAME_AUTO_UPDATE, 'page=optional'));
  exit;

}


if(isset($_GET['m']) && trim($_GET['m']!='')){

  $sql = "DELETE FROM `".TABLE_AUTO_UPDATE_AVAILABLE."` WHERE module='".htmlentities($_GET['m'], ENT_QUOTES)."'" ;

  $db->Execute($sql);

  header('Location: '.zen_href_link(FILENAME_AUTO_UPDATE));
  exit;

}




$page = htmlentities($_GET['page'], ENT_QUOTES);



$au = new httpClient;
$au->protocolVersion = '1.0' ;
$au->addHeader('Host', MODULE_AUTO_UPDATE_SERVER);
$au->addHeader('Connection', 'close');
$status = $au->Get('http://'.MODULE_AUTO_UPDATE_SERVER.'/'.MODULE_AUTO_UPDATE_URI.'?updates');

if($status != 200){

  $sql = "INSERT INTO ".TABLE_AUTO_UPDATE_LOG." (logged, error_type, message) VALUES (NOW(), 'fatal', 'Could not connect to update server when visiting the page at <strong>Admin >> Tools >> Automatic Updates</strong>. Webserver ".$status." error.') ";

  $db->execute($sql);


}


$sql = "SELECT au.module, au.version FROM ".TABLE_AUTO_UPDATE." au LEFT JOIN ".TABLE_AUTO_UPDATE_AVAILABLE." aul ON au.module=aul.replacing_module WHERE replacing_module IS NULL";

$q = $db->execute($sql);
$installed_modules = array();

while(!$q->EOF){
  $installed_modules[$q->fields['module']] = $q->fields['version'] ;
  $q->MoveNext();
}

$available_updates_html = '';

  foreach(explode("\n", str_replace("\r", "", $au->responseBody)) as $line){
    if(trim($line)!=''){
    $module = explode('|', $line);

      if(isset($installed_modules[$module[0]]) && version_compare($installed_modules[$module[0]], $module[1]) ){

	$sql = "INSERT INTO ".TABLE_AUTO_UPDATE_AVAILABLE." (replacing_module, replacing_version, module, version, status, md5key, url) VALUES ('".$module[0]."', '".$installed_modules[$module[0]]."', '".$module[0]."', '".$module[1]."', 'P', '".$module[3]."', '".$module[2]."')";

// 	$db->execute($sql);
	$available_updates_html .=  '<tr><td>'.$module[0].'</td><td>'.$module[1].'</td><td>Available</td></tr>';

      }else{

	$available_updates_html .=  '<tr><td>'.$module[0].'</td><td>'.$module[1].'</td><td>Installed</td></tr>';

      }

    }
  }

// include('update.php');


?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title>Admin :: <?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<link rel="stylesheet" type="text/css" href="includes/cssjsmenuhover.css" media="all" id="hoverJS">
<?php

  if ($handle = @opendir(DIR_FS_ADMIN.DIR_WS_INCLUDES.'/jscript')) {
      while (false !== ($tmpfile = readdir($handle))) {
        if ($tmpfile != "." && $tmpfile != "..") {
	  print '<script language="javascript" src="includes/jscript/'.$tmpfile.'" type="text/javascript"></script>';
        }
      }
        closedir($handle);
  }


?>
<script type="text/javascript">
  <!--
  function init()
  {
    cssjsmenu('navbar');
    if (document.getElementById)
    {
      var kill = document.getElementById('hoverJS');
      kill.disabled = true;
    }
  }
  // -->
</script>

<style type='text/css'>

.hddn{
  display:none;
}

.submit_link {
 color: #0000ff;
 background-color: transparent;
 text-decoration: none;
 border: none;
}
.su_admin_table{
border-style: collapse;
border: 1px solid #dddddd;
margin-left:1em;
}
.su_admin_table th {
background-color: #9a9aff;
}

.su_admin_table_stripe {
background-color: #dadaff;
}
h1 {margin-left: 0.5em;}
h2, h3, p{margin-left: 1em;}
.cntr{text-align: center}
</style>

</head>
<body onLoad="init()">
<!-- header //-->
<div class="header_area">
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
</div>
<!-- header_eof //-->
<div style="clear:both">
<ul style="background-color:#F5F5F5; border: solid #CCCCCC; border-width: 1px 0px;" class="toplinks">
  <li style="display:inline; padding:2px 5px; <?php echo ($page == '') ? 'background:#CCCCCC;' : ''; ?>">
    <a href="<?php echo zen_href_link(FILENAME_AUTO_UPDATE, '', 'SSL') ?>"><?php echo TEXT_LINK_DEFAULT_PAGE ; ?></a>
  </li>
  <li style="display:inline; padding:2px 5px; <?php echo ($page == 'logs') ? 'background:#CCCCCC;' : ''; ?>">
    <a href="<?php echo zen_href_link(FILENAME_AUTO_UPDATE, 'page=logs', 'SSL') ?>"><?php echo TEXT_LINK_DEFAULT_LOGS ; ?></a>
  </li>
</ul>

<h1><?php echo HEADING_TITLE ; ?></h1>

<?php
$conf = unserialize(MODULE_AUTO_UPDATE_CONF);

switch ($page){
  case 'logs':
    include(DIR_FS_ADMIN.'includes/su_auto_update_logs.php');
  break;
  case 'configuration':
//     include(DIR_FS_ADMIN.'includes/su_auto_update_configuration.php');
  break;
  default:
    include(DIR_FS_ADMIN.'includes/su_auto_update_available.php');
    include(DIR_FS_ADMIN.'includes/su_auto_update_optional.php');
}
?>


<script language="javascript">
$().ready(
  function(){
    $('.su_admin_table tr:odd').addClass('su_admin_table_stripe');
  }
);
</script>

</div>

<!-- footer //-->
<div class="footer-area">
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
</div>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>